package cate.game.role.res.farm;

import cate.common.table.d.GDMail;
import cate.common.table.d.GDOperation;
import cate.common.table.res.farm.row.FarmVipRow;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.GameBody;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.mail.Mail;
import easy.java.dev.note.NoteField;

import static cate.common.table.d.GDFarm.VipType;
import static cate.common.table.d.GDFarm.VipType.*;

public class FarmVip {

    @NoteField("当前激活的特权。 0 未激活特权, 1 月卡, 2 永久")
    public int vipType;

    @NoteField("特权过期时间，不过期：-1, 未激活特权时：0")
    public long expireTime;

    @NoteField(value = "上次领取奖励时间")
    public long lastRewardTime;

    @NoteField("月卡购买次数")
    public int buyTimes;

    public boolean vip() {
        return vipType > 0;
    }

    public VipType nowVipType() {
        return VipType.getVipType(vipType);
    }

    public GameResult<Void> allowPay(GameBody game, int tid) {
        GameResult<Void> r = new GameResult<>();
        FarmVipRow buyVipRow = game.table.farm.vip.get(tid);
        if(buyVipRow == null){
            return r.fail("不存在这个特权噢");
        }
        VipType buyVipType = getVipType(tid);
        if (buyVipType == null || buyVipType == NONE) {
            return r.fail("无法购买这个特权噢");
        }
        FarmVipRow nowVipRow = game.table.farm.vip.get(vipType);
        if (nowVipRow != null && nowVipRow.period == -1) {
            return r.fail("无需购买此特权拉");
        }
        VipType nowVipType = nowVipType();
        if (nowVipType == FOREVER) {
            return r.fail("无需购买此特权拉");
        }
        return r;
    }

    public void afterPay(GameBody game, int tid, MixRes reward, boolean direct) {
        VipType nowVipType = nowVipType();
        if (nowVipType == FOREVER) {
            return;
        }
        FarmVipRow buyVipRow = game.table.farm.vip.get(tid);
        if(buyVipRow == null){
            return;
        }
        VipType buyVipType = getVipType(tid);
        if (buyVipType == null || buyVipType == NONE) {
            return;
        }
        if (direct) {
            reward.addList(new MixRes(buyVipRow.buyRewardStr));
        }
        if (buyVipType == MONTH) {
            buyTimes ++;
            if (buyVipRow.changeTimes > 0 && buyTimes >= buyVipRow.changeTimes) {
                expireTime = -1;
                buyVipType = FOREVER;
            } else {
                if (expireTime > 0 && System.currentTimeMillis() < expireTime) {
                    expireTime = expireTime + TimeTool.TimeCons.DAY * buyVipRow.period;
                } else {
                    expireTime = System.currentTimeMillis() + TimeTool.TimeCons.DAY * buyVipRow.period;
                }
            }
        } else {
            expireTime = -1;
        }
        if (lastRewardTime == 0) {
            long zeroTime = TimeTool.getZeroTimeStamp(System.currentTimeMillis());
            lastRewardTime = zeroTime - 1000;
        }
        vipType = buyVipType.payId;
    }

    public boolean checkVip() {
        if (vipType > 0 && expireTime > 0 && System.currentTimeMillis() >= expireTime) {
            vipType = 0;
            expireTime = 0;
            return true;
        }
        return false;
    }

    public GameResult<Void> getVipDailyReward(Role role) {
        GameResult<Void> r = new GameResult<>();
        VipType nowVipType = nowVipType();
        if (nowVipType == NONE) {
            return r.fail("您未开通特权，无法领取奖励");
        }
        long zeroTime = TimeTool.getZeroTimeStamp(System.currentTimeMillis());
        if (lastRewardTime >= zeroTime) {
            return r.fail("您今日已经领取过奖励了");
        }
        FarmVipRow nowVipRow = role.getGame().table.farm.vip.get(MONTH.payId);
        if(nowVipRow == null){
            return r.fail("没有奖励可以领取");
        }
        lastRewardTime = System.currentTimeMillis();
        new MixResAdder()
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .setRes(nowVipRow.dailyRewardStr)
                .exe(role);
        return r;
    }

    public void onDaySpan(Role role) {
        VipType nowVipType = nowVipType();
        if (nowVipType == NONE) {
            return;
        }
        long zeroTime = System.currentTimeMillis();
        int day = TimeTool.getDistanceDays(lastRewardTime, zeroTime) - 1;
        if (day > 0 && vip()) {
            FarmVipRow nowVipRow = role.getGame().table.farm.vip.get(MONTH.payId);
            if(nowVipRow == null){
                return;
            }
            Mail mail = new MailBuilder()
                    .setContentId(GDMail.Tid.FARM_FORGET_VIP_REWARD)
                    .setReward(new MixRes(nowVipRow.dailyRewardStr).multiply(day))
                    .setOperation(GDOperation.FARM_DAILY_REWARD)
                    .build();
            role.getMail().add(mail);
        }
    }

    public void gmSetExpireTime(long expireTime) {
        this.expireTime = expireTime;
    }
}
